package com.sx.service.design;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sx.basic.BasicService;
import com.sx.basic.IBasicService;
import com.sx.basic.impl.BasicServiceImpl;
import com.sx.basic.pageable.Condition;
import com.sx.design.IDesignParamsService;
import com.sx.entity.design.DesignParamsEntity;
import com.sx.mapper.design.DesignParamsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author AdinZ
 * @date 2020/11/12 17:38
 */
@Service
public class DesignParamsService extends BasicServiceImpl<DesignParamsMapper,DesignParamsEntity>
        implements BasicService<DesignParamsEntity> , IDesignParamsService {

    /**
     * 根据组件模板id, 删除组件参数
     * @param moduleId
     */
    @Override
    public void deleteByModuleId(long moduleId) {
        this.delete(new Condition().eq("module_id",moduleId));
    }

    /**
     * 保存组件模板子参数
     * @param moduleId 组件模板id
     * @param list
     */
    @Override@Transactional
    public void putParams(long moduleId, List<DesignParamsEntity> list) {
        list.forEach(e -> {
            e.setModuleId(moduleId);
            e.setPid(0);
            this.save(e);
            if (e.getChild() != null && e.getChild().size() > 0)
                this.putParams(moduleId, e.getId(), e.getChild());
        });
    }

    @Override
    public List<DesignParamsEntity> findTree(long moduleId) {
        List<DesignParamsEntity> list = super.list(new QueryWrapper<DesignParamsEntity>().eq("module_id", moduleId)
                .orderByAsc("sort"));
        return DesignParamsEntity.toTree(list,0);
    }

    private void putParams(long moduleId,long pid,List<DesignParamsEntity> list) {
        list.forEach(e -> {
            e.setModuleId(moduleId);
            e.setPid(pid);
            this.save(e);
            if (e.getChild() != null && e.getChild().size() > 0) {
                this.putParams(moduleId, e.getId(), e.getChild());
            }
        });
    }
}
